package siwi.map.android;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.wmw.Utility.RegularExpression;

public class MyDialog {
	InputBox _inputBox;
	public void CreateADialog(final Activity activity, final TextView tv, int maxWordsCnt, final Sure sure){
		InputBox._MaxCntWords = maxWordsCnt;
		
		final Dialog dialog = new Dialog(activity, R.style.MyDialog);
		dialog.setContentView(R.layout.write_content);
		tv.setBackgroundColor(activity.getResources().getColor(R.color.TextBackground));
		dialog.setOnCancelListener(new OnCancelListener(){
			@Override
			public void onCancel(DialogInterface dialog) {
				tv.setBackgroundColor(activity.getResources().getColor(R.color.Transparent));
			}
		});
		
		Window window = dialog.getWindow();
		window.setWindowAnimations(R.style.dialogWindowAnim);
		window.setGravity(Gravity.TOP);
		window.setBackgroundDrawableResource(R.color.LightCyan);
		dialog.show();

		// 填充整个屏幕的宽度
		WindowManager windowManager = activity.getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.width = (int)(display.getWidth()); //设置宽度
		window.setAttributes(lp);
		
		final EditText _etDialog = (EditText)dialog.findViewById(R.id.W_C_EditText);
		_etDialog.setText(tv.getText());
		_etDialog.setSelection(_etDialog.length()); // Move to the last character.
		TextView tvDialog = (TextView)dialog.findViewById(R.id.W_C_Count);
		Button btnDialog = (Button)dialog.findViewById(R.id.W_C_Sure);
		btnDialog.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				sure.onClickSure(_etDialog.getText().toString());
				dialog.cancel();
			}
		});
		
		_inputBox = new InputBox(activity, _etDialog, null, tvDialog, _etDialog.getPaddingRight(), RegularExpression.Normal, new InputBox.NoAnyAction());
	}
	
	public interface Sure{
		public void onClickSure(String str);
	}
}
